1 /*
2  * Copyright (c) 2016 - Mauro Carvalho Chehab
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  */
18 
19 module libdvbv5_d.dvb_dev;
20 
21 import core.stdc.stdio;
22 
23 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
24 import libdvbv5_d.dvb_file: dvb_entry;
25 import libdvbv5_d.dvb_log: dvb_logfunc, dvb_logfunc_priv;
26 import libdvbv5_d.dvb_scan: dvb_v5_descriptors;
27 
28 import libdvbv5_d.linux_dmx: dmx_output_t, dmx_pes_type_t;
29 
30 extern (C):
31 
32 /**
33  * @file dvb-dev.h
34  * @ingroup dvb_device
35  * @brief Provides interfaces to handle Digital TV devices.
36  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
37  * @author Mauro Carvalho Chehab
38  *
39  * Digital TV device node file names depend on udev configuration. For
40  * example, while frontends are typically found at/dev/dvb/adapter?/frontend?,
41  * the actual file name can vary from system to system, depending on the
42  * udev ruleset.
43  *
44  * The libdvbv5 provides a set of functions to allow detecting and getting
45  * the device paths in a sane way, via libudev.
46  *
47  * @par Bug Report
48  * Please submit bug reports and patches to linux-media@vger.kernel.org
49  */
50 
51 /**
52  * @enum dvb_dev_type
53  *	@brief Type of a device entry to search
54  * @ingroup dvb_device
55  *
56  * @param DVB_DEVICE_FRONTEND	Digital TV frontend
57  * @param DVB_DEVICE_DEMUX	Digital TV demux
58  * @param DVB_DEVICE_DVR	Digital TV Digital Video Record
59  * @param DVB_DEVICE_NET	Digital TV network interface control
60  * @param DVB_DEVICE_CA		Digital TV Conditional Access
61  * @param DVB_DEVICE_CA_SEC	Digital TV Conditional Access serial
62  */
63 enum dvb_dev_type
64 {
65     DVB_DEVICE_FRONTEND = 0,
66     DVB_DEVICE_DEMUX = 1,
67     DVB_DEVICE_DVR = 2,
68     DVB_DEVICE_NET = 3,
69     DVB_DEVICE_CA = 4,
70     DVB_DEVICE_CA_SEC = 5
71 }
72 
73 /**
74  * @struct dvb_dev_list
75  *	@brief Digital TV device node properties
76  * @ingroup dvb_device
77  *
78  * @param path		path for the /dev file handler
79  * @param sysname	Kernel's system name for the device (dvb?.frontend?,
80  *			for example)
81  * @param dvb_type	type of the DVB device, as defined by enum dvb_dev_type
82  * @param bus_addr	address of the device at the bus. For USB devices,
83  *			it will be like: usb:3-1.1.4; for PCI devices:
84  *			pci:0000:01:00.0)
85  * @param bus_id	Id of the device at the bus (optional, PCI ID or USB ID)
86  * @param manufacturer	Device's manufacturer name (optional, only on USB)
87  * @param product	Device's product name (optional, only on USB)
88  * @param serial	Device's serial name (optional, only on USB)
89  */
90 struct dvb_dev_list
91 {
92     char* syspath;
93     char* path;
94     char* sysname;
95     dvb_dev_type dvb_type;
96     char* bus_addr;
97     char* bus_id;
98     char* manufacturer;
99     char* product;
100     char* serial;
101 }
102 
103 /**
104  * @enum dvb_dev_change_type
105  *	@brief Describes the type of change to be notifier_delay
106  *
107  * @param DVB_DEV_ADD		New device detected
108  * @param DVB_DEV_CHANGE	Device has changed something
109  * @param DVB_DEV_REMOVE	A hot-pluggable device was removed
110  */
111 enum dvb_dev_change_type
112 {
113     DVB_DEV_ADD = 0,
114     DVB_DEV_CHANGE = 1,
115     DVB_DEV_REMOVE = 2
116 }
117 
118 /**
119  * @brief Describes a callback for dvb_dev_find()
120  *
121  * sysname:	Kernel's system name for the device (dvb?.frontend?,
122  *			for example)
123  * @type:	type of change, as defined by enum dvb_dev_change_type
124  *
125  * @note: the returned string should be freed with free().
126  */
127 alias dvb_dev_change_t = int function (
128     char* sysname,
129     dvb_dev_change_type type,
130     void* priv);
131 
132 /**
133  * @struct dvb_open_descriptor
134  *
135  * Opaque struct with a DVB open file descriptor
136  */
137 struct dvb_open_descriptor;
138 
139 /**
140  * @struct dvb_device
141  *	@brief Digital TV list of devices
142  * @ingroup dvb_device
143  *
144  * @param devices	Array with a dvb_dev_list of devices. Each device
145  *			node is a different entry at the list.
146  * @param num_devices	number of elements at the devices array.
147  */
148 struct dvb_device
149 {
150     /* Digital TV device lists */
151     dvb_dev_list* devices;
152     int num_devices;
153 
154     /* Digital TV frontend access */
155     dvb_v5_fe_parms* fe_parms;
156 }
157 
158 /**
159  * @brief Allocate a struct dvb_device
160  * @ingroup dvb_device
161  *
162  * @note Before using the dvb device function calls, the struct dvb_device should
163  * be allocated via this function call.
164  *
165  * @return on success, returns a pointer to the allocated struct dvb_device or
166  *	NULL if not enough memory to allocate the struct.
167  */
168 dvb_device* dvb_dev_alloc ();
169 
170 /**
171  * @brief free a struct dvb_device
172  * @ingroup dvb_device
173  *
174  * @param dvb pointer to struct dvb_device to be freed
175  */
176 void dvb_dev_free (dvb_device* dvb);
177 
178 /**
179  * @brief finds all DVB devices on the local machine
180  * @ingroup dvb_device
181  *
182  * @param dvb			pointer to struct dvb_device to be filled
183  * @param enable_monitor	if different than zero put the routine into
184  *				monitor mode
185  * @param user_priv		pointer to user private data
186  *
187  * This routine can be called on two modes: normal or monitor mode
188  *
189  * In normal mode, it will seek for the local Digital TV devices, store them
190  * at the struct dvb_device and return.
191  *
192  * In monitor mode, it will not only enumerate all devices, but it will also
193  * keep waiting for device changes. The device seek loop will only be
194  * interrupted after calling dvb_dev_stop_monitor().
195  *
196  * Please notice that, in such mode, the function will wait forever. So, it
197  * is up to the application to put start a separate thread to handle it in
198  * monitor mode, and add the needed mutexes to make it thread safe.
199  *
200  * @return returns 0 on success, a negative value otherwise.
201  */
202 int dvb_dev_find (dvb_device* dvb, dvb_dev_change_t handler, void* user_priv);
203 
204 /**
205  * @brief Find a device that matches the search criteria given by this
206  *	functions's parameters.
207  *
208  * @param dvb		pointer to struct dvb_device to be used
209  * @param adapter	Adapter number, as defined internally at the Kernel.
210  *			Always start with 0;
211  * @param num		Digital TV device number (e. g. frontend0, net0, etc);
212  * @param type		Type of the device, as given by enum dvb_dev_type;
213  *
214  * @return returns a pointer to a struct dvb_dev_list object or NULL if the
215  *	desired device was not found.
216  */
217 dvb_dev_list* dvb_dev_seek_by_adapter (
218     dvb_device* dvb,
219     uint adapter,
220     uint num,
221     dvb_dev_type type);
222 
223 /**
224  * @brief Return data about a device from its sysname
225  *
226  * @param dvb		pointer to struct dvb_device to be used
227  * @param sysname	Kernel's name of the device to be opened, as obtained
228  *			via dvb_dev_seek_by_adapter() or via dvb_dev_find().
229  *
230  * @return returns a pointer to a struct dvb_dev_list object or NULL if the
231  *	desired device was not found.
232  */
233 dvb_dev_list* dvb_get_dev_info (dvb_device* dvb, const(char)* sysname);
234 
235 /**
236  * @brief Stop the dvb_dev_find loop
237  * @ingroup dvb_device
238  *
239  * @param dvb pointer to struct dvb_device to be used
240  *
241  * This function stops dvb_dev_find() if it is running in monitor
242  * mode. It does nothing on other modes. Can be called even if the
243  * monitor mode was already stopped.
244  */
245 void dvb_dev_stop_monitor (dvb_device* dvb);
246 
247 /**
248  * @brief Sets the DVB verbosity and log function with context private data
249  * @ingroup dvb_device
250  *
251  * @param dvb		pointer to struct dvb_device to be used
252  * @param verbose	Verbosity level of the messages that will be printed
253  * @param logfunc	Callback function to be called when a log event
254  *			happens. Can either store the event into a file or
255  *			to print it at the TUI/GUI. Can be null.
256  * @param logpriv   Private data for log function
257  *
258  * @details Sets the function to report log errors and to set the verbosity
259  *	level of debug report messages. If not called, or if logfunc is
260  *	NULL, the libdvbv5 will report error and debug messages via stderr,
261  *	and will use colors for the debug messages.
262  *
263  */
264 void dvb_dev_set_logpriv (
265     dvb_device* dvb,
266     uint verbose,
267     dvb_logfunc_priv logfunc,
268     void* logpriv);
269 
270 /**
271  * @brief Sets the DVB verbosity and log function
272  * @ingroup dvb_device
273  *
274  * @param dvb		pointer to struct dvb_device to be used
275  * @param verbose	Verbosity level of the messages that will be printed
276  * @param logfunc	Callback function to be called when a log event
277  *			happens. Can either store the event into a file or
278  *			to print it at the TUI/GUI. Can be null.
279  *
280  * @details Sets the function to report log errors and to set the verbosity
281  *	level of debug report messages. If not called, or if logfunc is
282  *	NULL, the libdvbv5 will report error and debug messages via stderr,
283  *	and will use colors for the debug messages.
284  *
285  */
286 void dvb_dev_set_log (dvb_device* dvb, uint verbose, dvb_logfunc logfunc);
287 
288 /**
289  * @brief Opens a dvb device
290  * @ingroup dvb_device
291  *
292  * @param dvb		pointer to struct dvb_device to be used
293  * @param sysname	Kernel's name of the device to be opened, as obtained
294  *			via dvb_dev_seek_by_adapter() or via dvb_dev_find().
295  * @param flags		Flags to be passed to open: O_RDONLY, O_RDWR and/or
296  *			O_NONBLOCK
297  *
298  *
299  * @note Please notice that O_NONBLOCK is not supported for frontend devices,
300  *	and will be silently ignored.
301  *
302  * @note the sysname will only work if a previous call to dvb_dev_find()
303  * 	is issued.
304  *
305  * @details This function is equivalent to open(2) system call: it opens a
306  *	Digital TV given by the dev parameter, using the flags.
307  *
308  * @return returns a pointer to the dvb_open_descriptor that should be used
309  * 	on further calls if sucess. NULL otherwise.
310  */
311 dvb_open_descriptor* dvb_dev_open (
312     dvb_device* dvb,
313     const(char)* sysname,
314     int flags);
315 
316 /**
317  * @brief Closes a dvb device
318  * @ingroup dvb_device
319  *
320  * @param open_dev	Points to the struct dvb_open_descriptor to be
321  * closed.
322  */
323 void dvb_dev_close (dvb_open_descriptor* open_dev);
324 
325 /**
326  * @brief returns fd from a local device
327  * This will not work for remote devices.
328  * @ingroup dvb_device
329  *
330  * @param open_dev	Points to the struct dvb_open_descriptor
331  *
332  * @return On success, returns the fd.
333  * Returns -1 on error.
334  */
335 int dvb_dev_get_fd (dvb_open_descriptor* open_dev);
336 
337 /**
338  * @brief read from a dvb demux or dvr file
339  * @ingroup dvb_device
340  *
341  * @param open_dev	Points to the struct dvb_open_descriptor to be
342  * closed.
343  * @param buf		Buffer to store the data
344  * @param count		number of bytes to read
345  *
346  * @return On success, returns the number of bytes read. Returns -1 on
347  * error.
348  */
349 size_t dvb_dev_read (dvb_open_descriptor* open_dev, void* buf, size_t count);
350 
351 /**
352  * @brief Stops the demux filter for a given file descriptor
353  * @ingroup dvb_device
354  *
355  * @param open_dev	Points to the struct dvb_open_descriptor
356  *
357  * This is a wrapper function for DMX_STOP ioctl.
358  *
359  * See http://linuxtv.org/downloads/v4l-dvb-apis/dvb_demux.html
360  * for more details.
361  *
362  * @note valid only for DVB_DEVICE_DEMUX.
363  */
364 void dvb_dev_dmx_stop (dvb_open_descriptor* open_dev);
365 
366 /**
367  * @brief Start a demux or dvr buffer size
368  * @ingroup dvb_device
369  *
370  * @param open_dev	Points to the struct dvb_open_descriptor
371  * @param buffersize	Size of the buffer to be allocated to store the filtered data.
372  *
373  * This is a wrapper function for DMX_SET_BUFFER_SIZE ioctl.
374  *
375  * See http://linuxtv.org/downloads/v4l-dvb-apis/dvb_demux.html
376  * for more details.
377  *
378  * @return Retuns zero on success, -1 otherwise.
379  *
380  * @note valid only for DVB_DEVICE_DEMUX or DVB_DEVICE_DVR.
381  */
382 int dvb_dev_set_bufsize (dvb_open_descriptor* open_dev, int buffersize);
383 
384 /**
385  * @brief Start a filter for a MPEG-TS Packetized Elementary
386  * 		       Stream (PES)
387  * @ingroup dvb_device
388  *
389  * @param open_dev	Points to the struct dvb_open_descriptor
390  * @param pid		Program ID to filter. Use 0x2000 to select all PIDs
391  * @param type		type of the PID (DMX_PES_VIDEO, DMX_PES_AUDIO,
392  *			DMX_PES_OTHER, etc).
393  * @param output	Where the data will be output (DMX_OUT_TS_TAP,
394  *			DMX_OUT_DECODER, etc).
395  * @param buffersize	Size of the buffer to be allocated to store the filtered data.
396  *
397  * This is a wrapper function for DMX_SET_PES_FILTER and DMX_SET_BUFFER_SIZE
398  * ioctls.
399  *
400  * See http://linuxtv.org/downloads/v4l-dvb-apis/dvb_demux.html
401  * for more details.
402  *
403  * @return Retuns zero on success, -1 otherwise.
404  *
405  * @note valid only for DVB_DEVICE_DEMUX.
406  */
407 int dvb_dev_dmx_set_pesfilter (
408     dvb_open_descriptor* open_dev,
409     int pid,
410     dmx_pes_type_t type,
411     dmx_output_t output,
412     int buffersize);
413 
414 /**
415  * @brief Sets a MPEG-TS section filter
416  * @ingroup dvb_device
417  *
418  * @param open_dev	Points to the struct dvb_open_descriptor
419  * @param pid		Program ID to filter. Use 0x2000 to select all PIDs
420  * @param filtsize	Size of the filter (up to 18 btyes)
421  * @param filter	data to filter. Can be NULL or should have filtsize length
422  * @param mask		filter mask. Can be NULL or should have filtsize length
423  * @param mode		mode mask. Can be NULL or should have filtsize length
424  * @param flags		flags for set filter (DMX_CHECK_CRC,DMX_ONESHOT,
425  *			DMX_IMMEDIATE_START).
426  *
427  * This is a wrapper function for DMX_SET_FILTER ioctl.
428  *
429  * See http://linuxtv.org/downloads/v4l-dvb-apis/dvb_demux.html
430  * for more details.
431  *
432  * @return Retuns zero on success, -1 otherwise.
433  *
434  * @note valid only for DVB_DEVICE_DEMUX.
435  */
436 int dvb_dev_dmx_set_section_filter (
437     dvb_open_descriptor* open_dev,
438     int pid,
439     uint filtsize,
440     ubyte* filter,
441     ubyte* mask,
442     ubyte* mode,
443     uint flags);
444 
445 /**
446  * @brief read the contents of the MPEG-TS PAT table, seeking for
447  *		      	an specific service ID
448  * @ingroup dvb_device
449  *
450  * @param open_dev	Points to the struct dvb_open_descriptor
451  * @param sid		Session ID to seeking
452  *
453  * @return At return, it returns a negative value if error or the PID
454  * associated with the desired Session ID.
455  *
456  * @warning This function currently assumes that the PAT fits into one session.
457  *
458  * @note valid only for DVB_DEVICE_DEMUX.
459  */
460 int dvb_dev_dmx_get_pmt_pid (dvb_open_descriptor* open_dev, int sid);
461 
462 /**
463  * @brief Scans a DVB dvb_add_scaned_transponder
464  * @ingroup frontend_scan
465  *
466  * @param entry		DVB file entry that corresponds to a transponder to be
467  * 			tuned
468  * @param open_dev	Points to the struct dvb_open_descriptor
469  * @param check_frontend a pointer to a function that will show the frontend
470  *			status while tuning into a transponder
471  * @param args		a pointer, opaque to libdvbv5, that will be used when
472  *			calling check_frontend. It should contain any parameters
473  *			that could be needed by check_frontend.
474  * @param other_nit	Use alternate table IDs for NIT and other tables
475  * @param timeout_multiply Improves the timeout for each table reception, by
476  *
477  * This is the function that applications should use when doing a transponders
478  * scan. It does everything needed to fill the entries with DVB programs
479  * (virtual channels) and detect the PIDs associated with them.
480  *
481  * This is the dvb_device variant of dvb_scan_transponder().
482  */
483 dvb_v5_descriptors* dvb_dev_scan (
484     dvb_open_descriptor* open_dev,
485     dvb_entry* entry,
486     int function () check_frontend,
487     void* args,
488     uint other_nit,
489     uint timeout_multiply);
490 
491 /* From dvb-dev-remote.c */
492 
493 /* 16356 bytes */
494 
495 /**
496  * @brief initialize the dvb-dev to use a remote device running the
497  *	dvbv5-daemon.
498  *
499  * @param dvb		pointer to struct dvb_device to be used
500  * @param server	server hostname or address
501  * @param port		server port
502  *
503  * @note The protocol between the dvbv5-daemon and the dvb_dev library is
504  * highly experimental and is subject to changes in a near future. So,
505  * while this is not stable enough, you will only work if both the client
506  * and the server are running the same version of the v4l-utils library.
507  */
508 
509 int dvb_dev_remote_init (dvb_device* d, char* server, int port);